<?php


namespace app\admin\controller;
use app\common\model\Attr as AT;
use think\facade\Env;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use think\facade\Session;
class Attr extends Base
{
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');

        $list = AT::where($where)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('group_name|group_title', "%$keyword%");
                }
            })
            ->order('group_sort asc')
            ->paginate(10, false, ['query' => request()->param()]);

        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
        ];
        View::assign($view);
        return View::fetch();
    }

    public function add(){
        if (Request::isPost()){
            $data = Request::post();
            //dump($data);die;
            $data['add_time'] = time();
            $prefix = Env::get('database.prefix');
            $goods = 'goods';
            $tablename = $prefix.$goods;
            $data['uid'] = Session::get('admin.admin_id');
            if (AT::create($data)){
                $sql="ALTER TABLE `$tablename` ADD COLUMN `".$data['screenid']."` int(10) DEFAULT 0 COMMENT '".$data['group_name']."';";
                Db::execute($sql);
                $this->success('添加筛选组名称:' . $data['group_name'].'添加成功', 'index');
            }else{
                $this->error('添加失败');
            }
        }else{
            $view = [
                'group_one' => null,
            ];
        }
        View::assign($view);
        return View::fetch();
    }

    public function edit(){
        $group_id = Request::param('group_id');
        //dump($group_id);die;
        if(Request::isPost()){
            $data = Request::post();
            $where['group_id'] = $group_id;
            $attr = Db::table('yuanma_attr')->where($where)->find();
            AT::update($data,$where);
            $prefix = Env::get('database.prefix');
            $goods = 'goods';
            $tablename = $prefix.$goods;
            $sql="ALTER TABLE `$tablename` CHANGE `".$attr['screenid']."` `".$data['screenid']."` int(10) DEFAULT 0 COMMENT '".$data['group_name']."';";
            Db::execute($sql);
            $this->success('修改成功!', 'index');
        }else{
            $group_one = Db::name('attr')->where(array('group_id'=>$group_id))->find();
            //dump($group_one);die;
            $view = [
                'group_id'=>$group_id,
                'group_one'=>$group_one,
            ];
        }
        View::assign($view);
        return View::fetch('add');
    }

    public function del(){
        $group_id = Request::param('group_id');
        $attr = Db::table('yuanma_attr')->where('group_id',$group_id)->find();
        Db::name('attrg')->where('group_id',$group_id)->delete();
        if (Db::name('attr')->delete($group_id)){
            $prefix = Env::get('database.prefix');
            $goods = 'goods';
            $tablename = $prefix.$goods;
            $sql="ALTER TABLE `$tablename` DROP COLUMN `".$attr['screenid']."`;";
            Db::execute($sql);
            return success('删除筛选组成功！','index');
        }else{
            return error('删除筛选组失败！');
        }
    }

    public function pildel(){
        $id = Request::param('id');
        $attr = Db::table('yuanma_attr')->where("group_id IN (".$id.")")->select();
        $ids = explode(',',$id);
        foreach ($ids as $k =>$v){
            Db::name('attrg')->where('group_id',$v)->delete();
            $res = Db::name('attr')->where('group_id',$v)->delete();
        }
        if($res){
            $prefix = Env::get('database.prefix');
            $goods = 'goods';
            $tablename = $prefix.$goods;
            foreach ($attr as $k =>$v){
                $sql="ALTER TABLE `$tablename` DROP COLUMN `".$v['screenid']."`;";
                Db::execute($sql);
            }
            return success('删除筛选组成功！','index');
        }else{
            return error('删除筛选组失败！');
        }
    }




    public function sort(){
        if (Request::isPost()) {
            $group_id = Request::param('group_id');
            $group_sort = Request::param('group_sort');
            if(false!== AT::where(array('group_id'=>$group_id))->update(['group_sort' => $group_sort])){
                $this->success('排序成功', 'index');
            }
        }
    }


    public function ajaxGroupName (){
        $key = Request::param('name');
        $value = Request::param('param');
        $group_id = input('group_id', 0, 'intval');
        $attr = new AT();
        $res = $attr->ajaxGroupNames($value, $group_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '筛选组英文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

    public function ajaxScreenName (){
        $key = Request::param('name');
        //dump($key);die;
        $value = Request::param('param');
        $group_id = input('group_id', 0, 'intval');
        $attr = new AT();
        $res = $attr->ajaxScreenNames($value, $group_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '自定义筛选ID名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

    public function ajaxGroupTitle (){
        $key = Request::param('name');
        $value = Request::param('param');
        $group_id = Request::param('group_id', 0, 'intval');
        $attr = new AT();
        $res = $attr->ajaxGroupTitles($value, $group_id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '筛选组中文名称已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }


}